<?php

class NewsController extends Zend_Controller_Action 
{

	/**
	 * @var Zend_Translate
	 */
	private $_translate;
	
	public function init() 
	{
		$this->_translate = Zend_Registry::get("Zend_Translate");
	
	}
	
	/**
     * Load News 
     * - Ajax action
     *  
     * @return void
     */
	public function loadnewslistAction() 
	{
		$this->_helper->layout->disableLayout();
        
		$feedOid = $this->_getParam('feedID');
		
		if (!empty($feedOid) && is_numeric($feedOid) && ($feedOid > 0)) {
		
			$feed = new Model_Entity_Feed();
			$feed->setOwners(Zend_Auth::getInstance()->getIdentity());
			$feed->setOid($feedOid);
			
	        $factory = Model_Factory::getInstance();
	        $service = $factory->getNewsService();
	        $news = $service->getUserNews($feed);
	                
	        $this->view->news = $news;
	        $this->view->displayLinks = true;
		} else {
		    $this->_helper->viewRenderer->setNoRender();
            $message = $this->_translate->translate('invalidparam');
            $this->_response->appendBody($message);
		}    
	}
	
   /**
     * Load News 
     * - Ajax action
     *  
     * @return void
     */
    public function loadsuggestnewslistAction() 
    {
        $this->_helper->layout->disableLayout();
        
        $feedOid = $this->_getParam('feedID');
        
        if (!empty($feedOid) && is_numeric($feedOid) && ($feedOid > 0)) {
        
            $feed = new Model_Entity_Feed();
            $feed->setOwners(Zend_Auth::getInstance()->getIdentity());
            $feed->setOid($feedOid);
            
            $factory = Model_Factory::getInstance();
            $service = $factory->getNewsService();
            $news = $service->getNews($feed);
                    
            $this->view->news = $news;
            $this->view->displayLinks = false;
            return $this->render('loadnewslist');
        } else {
            $this->_helper->viewRenderer->setNoRender();
            $message = $this->_translate->translate('invalidparam');
            $this->_response->appendBody($message);
        }    
    }
    
    /**
     * Load News 
     * - Ajax action
     *  
     * @return void
     */
    public function loadimportantnewslistAction() 
    {
        $this->_helper->layout->disableLayout();
            
        $factory = Model_Factory::getInstance();
        $service = $factory->getNewsService();
        
        $userID = Zend_Auth::getInstance()->getIdentity()->oid;
        
        $news = $service->getImportantNews($userID);
                    
        $this->view->news = $news;
        $this->view->displayLinks = true;
        
        return $this->render('loadnewslist');
    }
    
	/*
	 * Set News as important 
	 * - Ajax action
     *  
     * @return void
	 */
	public function setisimportantAction()
	{
		$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $localeMsg = $this->_translate->translate("ajaxError");
        
        $resultArray = array('error'   => true,
                             'message' => $localeMsg);
        
        $isImportant = $this->_getParam('valueToSet', 2);
        $newsID = $this->_getParam('elementID', 0);
        
        if (($isImportant >= 0 && $isImportant < 2) && 0 < $newsID) {
            
            $factory = Model_Factory::getInstance();
            $service = $factory->getNewsService();
                        
            $news = new Model_Entity_News();
            $news->setOid($newsID);
            $news->setIsImportant($isImportant);
            $news->setOwners(Zend_Auth::getInstance()->getIdentity());
            
            $service->setIsImportant($news);
            
            $resultArray = array('error' => false);
        }

        $return = Zend_Json::encode($resultArray);
        $this->_response->appendBody($return);
	}
	
	/**
     * Set News unread 
     * - Ajax action
     *  
     * @return void
     */
	public function setisreadAction()
	{
		$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $localeMsg = $this->_translate->translate("ajaxError");
        
        $resultArray = array('error'   => true,
                             'message' => $localeMsg);
        
        $isRead = $this->_getParam('valueToSet', 2);
        $newsID = $this->_getParam('elementID', 0);
        
        if (($isRead >= 0 && $isRead < 2) && 0 < $newsID) {
            
            $factory = Model_Factory::getInstance();
            $service = $factory->getNewsService();
                        
            $news = new Model_Entity_News();
            $news->setOid($newsID);
            $news->setIsRead($isRead);
            $news->setOwners(Zend_Auth::getInstance()->getIdentity());
            
            $service->setIsRead($news);
            
            $resultArray = array('error' => false);
        }

        $return = Zend_Json::encode($resultArray);
        $this->_response->appendBody($return);
	}
	
	/**
	 * Fetch news updates for user's feeds from internet
	 * - Ajax action
	 * 
	 * @return void
	 */
	public function upgradeusernewsAction()
	{
		$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $user = Zend_Auth::getInstance()->getIdentity();
        $userID = $user->getOid();
        
        $factory = Model_Factory::getInstance();
        $service = $factory->getFeedService();
        $newsService = $factory->getNewsService();
        
        $feeds = $service->getFeedToUpgrade($userID, 
                            FireFeed_Feed_Consumer::UPDATE_INTERVAL);

        foreach($feeds as $feed) {
            $news = FireFeed_Feed_Consumer::consumeFeed($feed);
            foreach($news as $item) {
            	$item->setOwners($user);
            	
                $id = $newsService->addNews($item);
                
                $item->setOid($id);
                $newsService->addUserNews($item);
            }
            if (!empty($news)) {
                $service->updateLastSync($feed->getOid());
            }	
        }
	}
	
	/**
     * Fetch news updates for boxes' feeds from internet
     * - Ajax action
     * 
     * @return void
	 */
	public function upgradeindexboxnewsAction()
	{
	    $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $factory = Model_Factory::getInstance();
        $service = $factory->getFeedService();
        $newsService = $factory->getNewsService();
        
        $feeds = $service->getFeedBoxToUpgrade( 
                            FireFeed_Feed_Consumer::UPDATE_INTERVAL);

        foreach($feeds as $feed) {
            $news = FireFeed_Feed_Consumer::consumeFeed($feed);
            foreach($news as $item) {
                $newsService->addNews($item);
            }
            if (!empty($news)) {
                $service->updateLastSync($feed->getOid());
            }   
        }
	}
	
	/**
	 * Delete a user's news
	 * - Ajax action
	 * 
	 * @return void
	 */
	public function deleteusernewsAction()
	{
		$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $localeMsg = $this->_translate->translate("ajaxError");
        
        $resultArray = array('error'   => true,
                             'message' => $localeMsg);
        $newsID = $this->_getParam('elementID', 0);
        
        if (0 < $newsID) {
            $factory = Model_Factory::getInstance();
            $service = $factory->getNewsService();
                        
            $news = new Model_Entity_News();
            $news->setOid($newsID);
            $news->setOwners(Zend_Auth::getInstance()->getIdentity());
            
            $service->deleteUserNews($news);
            
            $resultArray = array('error' => false);
        }

        $return = Zend_Json::encode($resultArray);
        $this->_response->appendBody($return);
	}
	
	/**
	 * Search in the user news
	 * - Ajax action
	 *  
	 * @return void
	 */
	public function searchusernewsAction()
	{
		$this->_helper->layout->disableLayout();
		
		$keywords = $this->_getParam('feedID', 0);
		
		if (!empty($keywords) && $keywords !== 0) {
			$userID = Zend_Auth::getInstance()->getIdentity()->oid;
			$criteria = array('userID' => $userID, 'contains' => $keywords);
			
			$factory = Model_Factory::getInstance();
            $service = $factory->getNewsService();
            
            $news = $service->searchUserNews($criteria);
            
			$this->view->news = $news;
			return $this->render('loadnewslist');
		} else {
			$this->_helper->viewRenderer->setNoRender();
            $message = $this->_translate->translate('invalidparam');
            $this->_response->appendBody($message);
		}
	}
}

